package com.nosnow.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.nosnow.common.utils.R;
import com.nosnow.handler.SnowflakeException;
import com.nosnow.vod.invoking.EduClient;
import com.nosnow.vod.service.VodService;
import com.nosnow.vod.utils.ConstantVideoUtils;
import com.nosnow.vod.utils.InitVodClient;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author by bingHeng
 * @version 1.0
 * @Create 2020/4/11 14:07
 */
@RestController
@RequestMapping("/vod/video")
public class VodController {
    @Autowired
    private VodService vodService;

    @Autowired
    private EduClient eduClient;

    @ApiOperation(value="上传视频到阿里视频点播")
    @PostMapping("uploadAlyVideo")
    public R uploadAlyVideo(MultipartFile file) {
        try {
            String videoId = vodService.uploadAlyVideo(file);
            return R.ok().data("videoId", videoId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
    @ApiOperation(value="根据视频Id，删除阿里云端视频")
    @DeleteMapping("removeAlyVideo/{id}/{videoSourceId}")
    public R removeAlyVideo(@PathVariable String id, @PathVariable String videoSourceId) {
        try {
            if(StringUtils.isEmpty(id)) {
                return R.error().data("20001", "小节Id为空");
            }

            if(StringUtils.isEmpty(videoSourceId)) {
                return R.error().data("20001", "视频Id为空");
            }

            vodService.deleteAlyVideo(videoSourceId);

            // TODO 用户在编辑页面删除阿里云视频时，可能直接点了取消按钮，这时候会造成数据库脏数据，所以在用户删除视频时，需要通过微服务去调用edu-service模块，更新相关表的数据
            eduClient.modifyVideoSourceIdByVideoId(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().data("20001", "删除视频失败");
        }
    }

    @ApiOperation(value="根据视频Id集合，删除阿里云端多个视频")
    @DeleteMapping("removeAlyVideos")
    public R removeAlyVideos(@RequestParam("videoIdList") List<String> videoIdList) {
        try {
            vodService.deleteAlyVideos(videoIdList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().data("20001", "批量删除视频失败");
        }
    }

    @ApiOperation(value="根据视频Id获取视频凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch(Exception e) {
            throw new SnowflakeException(20001,"获取凭证失败");
        }
    }
}
